$().ready(function() {
	validateRule();
});

$.validator.setDefaults({
	submitHandler : function() {
		update();
	}
});

//调用编辑页面的函数
function showTemplates(event,id) {
	event.preventDefault();
	layer.open({
		type : 2,
		title : '编辑',
		maxmin : true,
		shadeClose : false, // 点击遮罩关闭层
		area : [ '1000px', '550px' ],
		content : '../../../templates/ins/schedule/showTemp.html', //iframe的url
		success: function(layero, index){
			$.ajax({
				type : 'get',
				headers:{"JwtAuthorization":JwtAuthorizations,"Authorization":token},
				url : ip + 'ins/schedule/getById/'+ id,
				success : function(r) {
					parentId = r.parentId;
					name = r.name;
					orderNum = r.orderNum;
					delFlag = r.delFlag;
					var body = layer.getChildFrame('body',index);//建立父子联系
					var iframeWin = window[layero.find('iframe')[0]['name']];
					// console.log(arr); //得到iframe页的body内容
					// console.log(body.find('input'));
					var inputList = body.find('input');

//                                                alert(inputList.length);
					console.log(inputList);

					//$(body).find('input[id=id]').val(r.id);
					//$(body).find('input[id=name]').val(r.name);
					//$(body).find('input[id=value]').val(r.value);
					//$(body).find('input[id=type]').val(r.type);
					//$(body).find('input[id=description]').val(r.description);
					//$(body).find('input[id=sort]').val(r.sort);
					//$(body).find('input[id=remarks]').val(r.remarks);
				}
			});


		}
	});
}

//项目配置项
var openCiPopWin = function(id,version,name,multi,projectId,areaId){
	var contentUrl=SERVER_URL+"templates/popwin/cipopwin.html";
	if(multi){
		contentUrl=SERVER_URL+"templates/popwin/multicipopwin.html";
	}
	layer.open({
		type:2,
		title:"选择配置项",
		area : [ '600px', '400px' ],
		content:contentUrl,
		success: function(layero, index){
			var body = layer.getChildFrame('body',index);//建立父子联系
			$(body).find('input[id=idInput]').val(id);
			$(body).find('input[id=versionInput]').val(version);
			$(body).find('input[id=nameInput]').val(name);
			$(body).find('input[id=projectId]').val(projectId);
			$(body).find('input[id=areaId]').val(areaId);
			var iframeWin = window[layero.find('iframe')[0]['name']];
			iframeWin.init();
		}
	})
}

//选择项目
var openProjectPopWin = function(id,name,multi,customerId){
	var contentUrl=SERVER_URL+"templates/popwin/projectpopwin.html";
	if(multi){
		contentUrl=SERVER_URL+"templates/popwin/multiprojectpopwin.html";
	}
	layer.open({
		type:2,
		title:"选择项目",
		area : [ '600px', '400px' ],
		content:contentUrl,
		success: function(layero, index){
			var body = layer.getChildFrame('body',index);//建立父子联系
			$(body).find('input[id=idInput]').val(id);
			$(body).find('input[id=nameInput]').val(name);
			$(body).find('input[id=customerId]').val(customerId);
			var iframeWin = window[layero.find('iframe')[0]['name']];
			iframeWin.init();
		}
	})
}

function update() {
	$.ajax({
		cache : true,
		type : "POST",
		url : "/ins/schedule/update",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			}
		},
		messages : {
			name : {
				required : icon + "请输入名字"
			}
		}
	})
}